package com.dongdong.service.impl;

import com.dongdong.mapper.TbSpecificationOptionMapper;
import com.dongdong.model.TbSpecificationOption;
import com.dongdong.model.TbSpecificationOptionExample;
import com.dongdong.service.SpecOptionService;
import com.dongdong.vo.SpecOptionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpecOptionServiceImpl implements SpecOptionService {

    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    /**
     * 根据ID查询规格选项表
     * @param specId
     * @return
     */
    @Override
    public List<SpecOptionVO> SelectSpecOptionBySpecId(Long specId) {
        TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
        tbSpecificationOptionExample.createCriteria().andSpecIdEqualTo(specId);
        List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationOptionMapper.selectByExample(tbSpecificationOptionExample);
        List<SpecOptionVO> vos = tbSpecificationOptions.stream().map((x) -> {
            SpecOptionVO specOptionVO = new SpecOptionVO();
            BeanUtils.copyProperties(x, specOptionVO);
            return specOptionVO;
        }).collect(Collectors.toList());
        return vos;
    }

    /**
     * 增加规格选项
     * @param specOptionVO
     */
    @Override
    public void InsertSpecOption(List<SpecOptionVO> specOptionVO) {
        List<TbSpecificationOption> tbSpecificationOptions = specOptionVO.stream().map((x) -> {
            TbSpecificationOption tbSpecificationOption = new TbSpecificationOption();
            BeanUtils.copyProperties(x, tbSpecificationOption);
            return tbSpecificationOption;
        }).collect(Collectors.toList());
        for (TbSpecificationOption tbSpecificationOption : tbSpecificationOptions) {
            tbSpecificationOptionMapper.insertSelective(tbSpecificationOption);
        }
    }

    /**
     * 删除该ID下所有规格选项数据
     * @param specId
     */
    @Override
    public void DeleteSpecOptionBySpecId(Long specId) {
        TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
        tbSpecificationOptionExample.createCriteria().andSpecIdEqualTo(specId);
        tbSpecificationOptionMapper.deleteByExample(tbSpecificationOptionExample);
    }
}
